﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Reflection;

namespace ActiveXTree
{
    [ComVisible(true)]
    [InterfaceType(ComInterfaceType.InterfaceIsDual)]
    [Guid("E66C39CB-BB8B-4738-AA0E-5E0D1F2DB230")]
    interface IViewTree
    {
    } 
    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.None)]
    [Guid("D100C392-030A-411C-92B6-4DBE9AC7AA5A")]
    [ProgId("CSActiveXCtrl")]
    [ComDefaultInterface(typeof(IViewTree))]
    public partial class CSActiveXCtrl : UserControl,IViewTree
    {
        public CSActiveXCtrl()
        {
            InitializeComponent();
            _drawtree = new DrawTree();
            splitContainer1.Panel2.Controls.Add(_drawtree);
            _drawtree.Dock = DockStyle.Fill;
        }
        private DrawTree _drawtree = new DrawTree();
        private BTreeWithRef _bt = new BTreeWithRef();
        void InitializeGB(bool add, bool del, bool search,bool view)
        {
            GB_Add.Visible = add;
            GB_DelNode.Visible = del;
            GB_Search.Visible = search;
            GB_View.Visible = view;
            
        }

        void createNewTreeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _bt = new BTreeWithRef();
            _drawtree.Draw(_bt.root);
            InitializeGB(false, false, false,false );
        }

        void addNewElementToolStripMenuItem_Click(object sender, EventArgs e)
        {
            InitializeGB(true, false, false,false );

        }

        void Btn_Search_Click(object sender, EventArgs e)
        {
            treeNode tmp=_bt.Search(Convert.ToInt16(MTB_Search.Text));
            _drawtree.Draw(tmp);
        }

        void Btn_DelNode_Click(object sender, EventArgs e)
        {
            _bt.DelNode(Convert.ToInt16(MTB_DelNode.Text));
            _drawtree.Draw(_bt.root);
        }

        void Btn_AddNode_Click(object sender, EventArgs e)
        {
            _bt.AddNode(Convert.ToInt16(MTB_AddNode.Text));
            _drawtree.Draw(_bt.root);
        }

        void nodeSearchToolStripMenuItem_Click(object sender, EventArgs e)
        {
            InitializeGB(false, false, true,false);
        }

        void elementRemoveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            InitializeGB(false , true , false,false);
        }

        void heightToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Height of BTree = " + _bt.Height().ToString() + ".", "HEIGHT");
        }

        void widthToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Width of BTree = " + _bt.Width().ToString() + ".", "WIDHT");
        }

        void amountOfLeavesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Amount of leaves = " + _bt.AmountOfLeaves().ToString() + ".", "LEAVES");
        }

        void amountOfNodesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Amount of nodes = " + _bt.AmountOfNodes().ToString() + ".", "NODES");
        }

        void totalAmountOfElementsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Total amount of nodes = " + _bt.TotalAmountOfNodes().ToString() + ".", "TOTAl AMOUNT");
        }

        void reversToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _bt.Revers();
            _drawtree.Draw(_bt.root);
        }

        void viewSortElementsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            InitializeGB(false, false, false, true);
            TB_View.Items.Clear();
            List<int> values = _bt.SortValuePrint();
            for (int i=0;i<values.Count;i++)
            {
                
                TB_View.Items.Add(values[i].ToString());
            }
        }

        [ComRegisterFunction]
        public static void RegisterClass(string key)
        {
            // Strip off HKEY_CLASSES_ROOT\ from the passed key as I don't need it
            StringBuilder sb = new StringBuilder(key);

            sb.Replace(@"HKEY_CLASSES_ROOT\", "");
            // Open the CLSID\{guid} key for write access
            RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(), true);

            // And create	the	'Control' key -	this allows	it to show up in
            // the ActiveX control container
            RegistryKey ctrl = k.CreateSubKey("Control");
            ctrl.Close();

            // Next create the CodeBase entry	- needed if	not	string named and GACced.
            RegistryKey inprocServer32 = k.OpenSubKey("InprocServer32", true);
            inprocServer32.SetValue("CodeBase", Assembly.GetExecutingAssembly().CodeBase);
            inprocServer32.Close();
            // Finally close the main	key
            k.Close();
            MessageBox.Show("Registered");
        }

        [ComUnregisterFunction]
        public static void UnregisterClass(string key)
        {
            StringBuilder sb = new StringBuilder(key);
            sb.Replace(@"HKEY_CLASSES_ROOT\", "");

            // Open	HKCR\CLSID\{guid} for write	access
            RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(), true);

            // Delete the 'Control'	key, but don't throw an	exception if it	does not exist
            k.DeleteSubKey("Control", false);

            // Next	open up	InprocServer32
            //RegistryKey	inprocServer32 = 
            k.OpenSubKey("InprocServer32", true);

            // And delete the CodeBase key,	again not throwing if missing
            k.DeleteSubKey("CodeBase", false);

            // Finally close the main key
            k.Close();
            MessageBox.Show("UnRegistered");
        }




    }
}
